/*******************************************
 * Copyright 2007 Aphrodite Framework for javaME .
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * Email : owner@aphroditeme.dev.java.net
 * Contributors:
 *     Kent Elton - initial API and implementation
 *******************************************/

package com.aphrodite.util;

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

/**
 * Operations performed on collection classes
 * 
 * @author kentelt
 * 
 */
public final class CollectionUtil {

	/**
	 * returns a copy of a hashtable
	 * 
	 * @param source
	 * @return Copy of <code>source</code>
	 */
	public static Hashtable cloneHashtable(Hashtable source) {
		Enumeration aKeys = source.keys();
		Hashtable to = new Hashtable();
		while (aKeys.hasMoreElements()) {
			Object rKey = aKeys.nextElement();
			to.put(rKey, source.get(rKey));
		}
		return to;
	}

	/**
	 * returns a copy of the vector
	 * 
	 * @param source
	 * @return Copy of <code>source</code>
	 */
	public static Vector cloneVector(Vector source) {
		int nCount = source.size();
		Vector to = new Vector();
		for (int i = 0; i < nCount; i++) {
			to.addElement(source.elementAt(i));
		}
		return to;
	}

	public static Object[] toArray(Vector vector) {
		Object[] obj = new Object[vector.size()];
		vector.copyInto(obj);
		return obj;

	}

	/**
	 * Converts an array of objects to a vector
	 * 
	 * @param array
	 * @return Vector containing the objects in <code>array</code>
	 */
	public static Vector toVector(final Object[] array) {
		if (array == null) {
			return null;
		} else {
			final int length = array.length;
			final Vector v = new Vector(length);
			for (int i = 0; i < length; i++) {
				v.addElement(array[i]);
			}
			return v;
		}
	}
}
